package com.pwser.entities;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserDetailsImpl implements UserDetails {
	
	private static final long serialVersionUID = 1L;

	private String loginAccount;
	
	private String loginPass;
	
	private String loginStatus;
	
    //包含着用户对应的所有Role，在使用时调用者给对象注入roles
    private List<Role> roles;

    
    //返回用户所有角色的封装，一个Role对应一个GrantedAuthority
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> authorities = new ArrayList<>();
        for(Role role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getRoleNo()));
        }
        return authorities;
	}

	@Override
	public String getPassword() {
		return loginPass;
	}

	@Override
	public String getUsername() {
		return loginAccount;
	}
	
	//判断账号是否已经过期，默认没有过期
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}
	
	//判断账号是否被锁定
	@Override
	public boolean isAccountNonLocked() {
		if(loginStatus.equals("0")) {
			return true;
		}else {
			return false;
		}
	}
	
	//判断信用凭证是否过期，默认没有过期
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}
	
	//判断账号是否可用，默认可用
	@Override
	public boolean isEnabled() {
		return true;
	}

}
